import { WinsleiLogManager } from '@winslei/winsleilog'

const TAG = 'ColorUtil'

export class ColorUtil {
  constructor() {
    WinsleiLogManager.hilogIWithTag(TAG, `constructor`)
  }

  randomColorWithOpcity(): string {
    return this.RGBAToHex(`rgba(${this.randomNumber(255, 0)},${this.randomNumber(255, 0)},${this.randomNumber(255, 0)},${this.randomNumber(255, 0) / 255})`)
  }

  randomColor(): string {
    return this.RGBToHex(`rgb(${this.randomNumber(255, 0)},${this.randomNumber(255, 0)},${this.randomNumber(255, 0)})`)
  }

  // 入参格式: 'rgba(255,255,255,1)', 返回格式：'#ffffffff'
  RGBAToHex(rgba: string): string {
    let str = rgba.slice(5, rgba.length - 1),
      arry = str.split(','),
      opa = Number(arry[3].trim()) * 255,
      strHex = "#",
      r = Number(arry[0].trim()),
      g = Number(arry[1].trim()),
      b = Number(arry[2].trim());

    strHex += opa.toString(16) + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

    return strHex;
  }

  // 入参格式: 'rgb(255,255,255)', 返回格式：'#ffffff'
  RGBToHex(rgb: string): string {
    let str = rgb.slice(4, rgb.length - 1),
      arry = str.split(','),
      strHex = "#",
      r = Number(arry[0].trim()),
      g = Number(arry[1].trim()),
      b = Number(arry[2].trim());

    strHex += ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

    return strHex;
  }

  // 获取随机数
  private randomNumber(max: number, min: number): number {
    return Math.round(Math.random() * (min - max) + max);
  }
}